close all

%% MAZE LEARNING

normal = dlmread('learn_tiny_1p_200eps_grid.csv');
sticky = dlmread('learn_tiny_1p_200eps_grid_sticky.csv');

normal = mean(normal,1);
sticky = mean(sticky,1);

normal = normal(:,1:120);
sticky = sticky(:,1:120);

figure('Color',[1 1 1]); 
hold on
plot(normal, 'b-', 'LineWidth',1)
plot(sticky, 'r--', 'LineWidth',1)
xlabel('Episode')
ylabel('Average score')
title('Maze learning with/without new physics.')
legend('New','Old (sticky)','Location','East')

set(gcf,'Position',[150 400 600 250]);
f = getframe(gcf);
imwrite(f.cdata,'fig_learn_maze.png');

%% 1 vs 1


h00 = dlmread('learn_tiny_1v1_30000eps.csv');
h05 = dlmread('learn_tiny_1v1_30000eps_h5.csv');
h10 = dlmread('learn_tiny_1v1_30000eps_h10.csv');
h15 = dlmread('learn_tiny_1v1_30000eps_h15.csv');
h20 = dlmread('learn_tiny_1v1_30000eps_h20.csv');

h00 = mean(h00,1);
h05 = mean(h05,1);
h10 = mean(h10,1);
h15 = mean(h15,1);
h20 = mean(h20,1);

% h00 = smooth(h00,n);
% h05 = smooth(h05,n);
% h10 = smooth(h10,n);
% h15 = smooth(h15,n);
% h20 = smooth(h20,n);
n = 10;
e = 100;
h00 = h00(1:n:e);
h05 = h05(1:n:e);
h10 = h10(1:n:e);
h15 = h15(1:n:e);
h20 = h20(1:n:e);

figure('Color',[1 1 1]); 
hold on
plot(h00,'r');
plot(h05,'b');
plot(h10,'g');
plot(h15,'k');
plot(h20,'c');
xlabel('Episode')
ylabel('Average score')
title('Learning 1 vs 1 with different handicaps.')
legend('0.00','0.05','0.10','0.15','0.20');

set(gcf,'Position',[150 400 600 250]);
f = getframe(gcf);
imwrite(f.cdata,'fig_learn_1v1_handicaps.png');

%% 1v1 grid

grid = dlmread('learn_tiny_1v1_30000eps_h20_grid.csv');
nogrid = dlmread('learn_tiny_1v1_30000eps_h20.csv');

grid = mean(grid,1);
nogrid = mean(nogrid,1);


n = 10000;
grid = smooth(grid,n);
nogrid = smooth(nogrid,n);

figure('Color',[1 1 1]); 
hold on
plot(grid(1:end-100),'r','LineWidth',2);
plot(nogrid(1:end-100),'b:','LineWidth',2);
xlabel('Episode')
ylabel('Average score')
title('Learning 1 vs 1 with different handicaps.')
legend('Grid','Points of Interest','Location','Best');

set(gcf,'Position',[150 400 600 250]);
f = getframe(gcf);
imwrite(f.cdata,'fig_learn_1v1_grid.png');


%% 2vs2
h00 = dlmread('learn_tiny_2v2_15000eps.csv');
h05 = dlmread('learn_tiny_2v2_15000eps_h5.csv');
h10 = dlmread('learn_tiny_2v2_15000eps_h10.csv');
h15 = dlmread('learn_tiny_2v2_15000eps_h15.csv');
h20 = dlmread('learn_tiny_2v2_15000eps_h20.csv');

h00 = mean(h00,1);
h05 = mean(h05,1);
h10 = mean(h10,1);
h15 = mean(h15,1);
h20 = mean(h20,1);

n = 1000;
h00 = smooth(h00,n);
h05 = smooth(h05,n);
h10 = smooth(h10,n);
h15 = smooth(h15,n);
h20 = smooth(h20,n);

figure('Color',[1 1 1]); 
hold on
plot(h00(1:end-100),'k:');
plot(h05(1:end-100),'k-.');
plot(h10(1:end-100),'k');
plot(h15(1:end-100),'k:','LineWidth',2);
plot(h20(1:end-100),'k-','LineWidth',2);

xlabel('Episode')
ylabel('Average score')
title('Learning 2 vs 2.')
legend('0.00','0.05','0.10','0.15','0.20','Location','NorthWest');

set(gcf,'Position',[150 400 600 250]);
f = getframe(gcf);
imwrite(f.cdata,'fig_learn_2v2.png');

%% 2v2 Majority vs Normal
default = dlmread('learn_tiny_2v2_15000eps_h10.csv');
majority = dlmread('learn_tiny_2v2_15000eps_h10_majority.csv');

default = mean(default,1);
majority = mean(majority,1);

default = smooth(default,1000);
majority = smooth(majority,1000);

figure('Color',[1 1 1]); 
hold on
plot(default(1:end-100),'k--');
plot(majority(1:end-100),'k');

xlabel('Episode')
ylabel('Average score')
title('Learning 2 vs 2 with different capture modes.')
legend('Neutral','Majority','Location','NorthWest');

set(gcf,'Position',[150 400 600 250]);
f = getframe(gcf);
imwrite(f.cdata,'fig_learn_2v2_capturemode.png');



%% STABILITY VS OLD
old = dlmread('scores_old.csv');
old = old(1,2:201);
scores = dlmread('scores_default.csv');
scores = scores(1,2:end);

ho = hist(old);
hs = hist(scores);

figure('Color',[1 1 1]); 
bar(ho)
title('Old game');
set(gca,'XTickLabel',[0:0.1:1])
xlabel('Score')

set(gcf,'Position',[150 400 400 150]);
f = getframe(gcf);
imwrite(f.cdata,'fig_stability_old.png');

figure('Color',[1 1 1]); 
bar(hs)
title('New game');
set(gca,'XTickLabel',[0:0.1:1])
xlabel('Score')

set(gcf,'Position',[150 400 400 150]);
f = getframe(gcf);
imwrite(f.cdata,'fig_stability_new.png');

%% STABILITY 

% 3D FIG
curfig = figure('Color',[1 1 1]); 
subplot(1,2,1)
scores = dlmread('scores_respawn_1.csv');
handicaps = scores(:,1);
scores = scores(:,2:end);
colormap hot
h = hist(scores');
bg = bar3(h);

title('Fast Respawn')
xlabel('Handicap')
ylabel('Score')
zlabel('Amount')
set(gca,'XTickLabel',handicaps')
set(gca,'YTickLabel',[0:0.1:1])
set(gca,'CLim',[0,50])

set(gcf,'Position',[150 400 1000 400]);
view([-47,40]);

% Tell handle graphics to use interpolated rather than flat shading
shading interp
% For each barseries, map its CData to its ZData
for i = 1:length(bg)
    zdata = get(bg(i),'ZData');
    set(bg(i),'CData',zdata)
    % Add back edge color removed by interpolating shading
    set(bg,'EdgeColor','k') 
end

subplot(1,2,2)
scores = dlmread('scores_respawn_40_ammorate_40.csv');
handicaps = scores(:,1);
scores = scores(:,2:end);
% colormap hot
h = hist(scores');
bg = bar3(h);

title('Slow Respawn')
xlabel('Handicap')
ylabel('Score')
zlabel('Amount')
set(gca,'XTickLabel',handicaps')
set(gca,'YTickLabel',[0:0.1:1])
set(gca,'CLim',[0,50])
view([-47,40]);

% Tell handle graphics to use interpolated rather than flat shading
shading interp
% For each barseries, map its CData to its ZData
for i = 1:length(bg)
    zdata = get(bg(i),'ZData');
    set(bg(i),'CData',zdata)
    % Add back edge color removed by interpolating shading
    set(bg,'EdgeColor','k') 
end

f = getframe(gcf);
imwrite(f.cdata,'fig_stability_3dbars.png');

%% COMPARISON FIGS
ls = {'b-','r:','g--','k-.'};
scores_default = dlmread('scores_default.csv');
scores_moreammo = dlmread('scores_ammorate_1.csv');
scores_fastspawn = dlmread('scores_respawn_1.csv');
scores_slowspawn = dlmread('scores_respawn_40_ammorate_40.csv');
handicaps = scores_default(:,1);
scores_default = scores_default(:,2:end);
scores_moreammo = scores_moreammo(:,2:end);
scores_fastspawn = scores_fastspawn(:,2:end);
scores_slowspawn = scores_slowspawn(:,2:end);
lims = [min(handicaps) max(handicaps)]

figure('Color',[1 1 1]); 
title('Average score with different settings')
hold on;
plot(handicaps,mean(scores_default,2),ls{1},'LineWidth',2)
plot(handicaps,mean(scores_moreammo,2),ls{2},'LineWidth',2)
plot(handicaps,mean(scores_fastspawn,2),ls{3},'LineWidth',2)
plot(handicaps,mean(scores_slowspawn,2),ls{4},'LineWidth',2)
legend('Default','More Ammo','Fast Spawn','Slow Spawn/Ammo');
ylabel('Mean Score')
ylabel('Score Stdev')
xlim(lims)

set(gcf,'Position',[150 400 450 230]);
f = getframe(gcf);
imwrite(f.cdata,'fig_stability_settings_avg.png');

pause(1)

figure('Color',[1 1 1]); 
title('Standard Deviation of score with different settings')
hold on;
plot(handicaps,std(scores_default,0,2),ls{1},'LineWidth',2)
plot(handicaps,std(scores_moreammo,0,2),ls{2},'LineWidth',2)
plot(handicaps,std(scores_fastspawn,0,2),ls{3},'LineWidth',2)
plot(handicaps,std(scores_slowspawn,0,2),ls{4},'LineWidth',2)
ylabel('Score Stdev')
xlabel('Handicap')
xlim(lims)

set(gcf,'Position',[150 400 450 230]);
pause(1);
f = getframe(gcf);
imwrite(f.cdata,'fig_stability_settings_std.png');


%% CAPTURE MODE
ls = {'b-','r:','g--','k-.'};

scores_default = dlmread('scores_default.csv');
scores_majority = dlmread('scores_cp_majority.csv');
scores_first = dlmread('scores_cp_persistent.csv');

handicaps = scores_default(:,1);
scores_default = scores_default(:,2:end);
scores_majority = scores_majority(:,2:end);
scores_first = scores_first(:,2:end);

figure('Color',[1 1 1]); 
title('Average score with different capture modes')
hold on;
plot(handicaps,mean(scores_default,2),ls{1},'LineWidth',2)
plot(handicaps,mean(scores_majority,2),ls{2},'LineWidth',2)
plot(handicaps,mean(scores_first,2),ls{3},'LineWidth',2)

legend('Neutral','Majority Wins','First Wins');
ylabel('Mean Score');
xlabel('Handicap');

set(gcf,'Position',[150 400 450 230]);
pause(1)
f = getframe(gcf);
imwrite(f.cdata,'fig_stability_capmode_avg.png');

figure('Color',[1 1 1]); 
title('Score standard deviation with different capture modes')
hold on;
plot(handicaps,std(scores_default,0,2),ls{1},'LineWidth',2)
plot(handicaps,std(scores_majority,0,2),ls{2},'LineWidth',2)
plot(handicaps,std(scores_first,0,2),ls{3},'LineWidth',2)
ylabel('Score Stdev')
xlabel('Handicap')

set(gcf,'Position',[150 400 450 230]);
pause(1)
f = getframe(gcf);
imwrite(f.cdata,'fig_stability_capmode_std.png');

3+3

